var editQuestionnaireTmpl = '问卷标题：<input type="text" name="qnName" id="qnName" value="#questionnaireName#" /><br/>'+
			'问卷描述：<input type="text" name="qnDescription" id="qnDescription" value="#questionnaireDescription#" /><br/>'+
			'问卷分类：<select name="qnGroup" id="qnGroup">'+
				'<option value="1"#questionnaireGroup1#>娱乐</option>'+
				'<option value="2"#questionnaireGroup2#>生活</option>'+
				'<option value="3"#questionnaireGroup3#>校园</option>'+
			'</select>问卷类型：<select name="qnType" id="qnType">'+
				'<option value="1"#questionnaireType1#>标准</option>'+
				'<option value="2"#questionnaireType2#>PK台</option>'+
			'</select><br/><input type="button" name="button" onClick="submitQuestionnaire()" value="#buttonValue#">';
			
var editQuestionTmpl = '问题题目：<input type="text" id="qTitle" value="#questionTitle#" /><br/>问题类型：<select id="qType">'+
			'<option value="1" #questionType1#>单选</option>'+
			'<option value="2" #questionType2#>多选</option>'+
			'</select><ul id="option_edit">#optionList#</ul><br/><input type="button" onClick="addOption()" value="添加选项"/>'+
			'<br/><input type="button" name="button" onClick="submitQuestion()" value="#buttonValue#">'+
			'<input type="button" name="button" onClick="cancelQuestion()" value="取消">';
			
var editOptionTmpl = '<li>选项#No#：<input type="text" name="edit_option" value="#value#"/></li>';

var displayQuestionnaireTmpl = '<div id="dqnHead"><h2>#questionnaireName#</h2><h3>#questionnaireDescription#</h3></div>'+
								'<div id="dqList">#questionList#</div>';

var displayQuestionTmpl = '<div class="dqNode"><div class="dqTitle">#questionTitle#</div><div class="doList">#optionList#</div>'+
						'<input type="button" onClick="deleteQuestion(#questionIndex#)" value="删除">#ctrl#</div>';

var displayOptionTmpl = '<div class="#optionClass#"><input type="#optionType#" name="#optionName#">#optionText#</div>';


var questionnaire = getNewQuestionnaire();
var editing_question_id;
var editing_question;
var isNewQuestion = true;

function editQuestionnaire(i){
	var inner = new String('');
	inner = editQuestionnaireTmpl;
	inner = inner.replace('#questionnaireName#', questionnaire.questionnaireName);
	inner = inner.replace('#questionnaireDescription#', questionnaire.questionnaireDescription);
	inner = inner.replace('#buttonValue#', (i == 1 ? '创建' : '修改'));
	inner = inner.replace('#questionnaireGroup'+questionnaire.questionnaireGroup+'#', ' selected="selected"');
	inner = inner.replace(/#questionnaireGroup\d+#/, '');
	inner = inner.replace('#questionnaireType'+questionnaire.questionnaireType+'#', ' selected="selected"');
	inner = inner.replace(/#questionnaireType\d+#/, '');
	
	document.getElementById('questionnaire_edit_panel').innerHTML = inner;
	return;
}

function addQuestion(i){
	if(i == -1){
		editing_question_id = questionnaire.questionList.length;
		editing_question = getNewQuestion();
		editing_question.optionList[editing_question.optionList.length] = getNewOption();
		isNewQuestion = true;
	}
	else{
		editing_question = questionnaire.questionList[i];
		editing_question_id = i;
		isNewQuestion = false;
	}
	display_edit_question();
	return;
}

function cancelQuestion(){
	var inner = new String('');
	inner = '<input type="button" value="添加问题" onClick="addQuestion(-1)"/>';
	document.getElementById('questionnaire_edit_panel').innerHTML = inner;
}

function addOption(){
	cacheOption();
	editing_question.optionList[editing_question.optionList.length] = getNewOption();
	display_edit_question();
	return;
}

function submitQuestionnaire(){
	questionnaire.questionnaireName = document.getElementById('qnName').value;
	questionnaire.questionnaireDescription = document.getElementById('qnDescription').value;
	questionnaire.questionnaireGroup = document.getElementById('qnGroup').value;
	questionnaire.questionnaireType = document.getElementById('qnType').value;
	display_questionnaire();
	addQuestion(-1)
	return;
}

function submitQuestion(){
	cacheOption();
	if(editing_question.questionTitle.trim() == ''){
		alert('请填写问题题目');
		return;
	}
	if(editing_question.optionList.length == 0 || (editing_question.optionList.length == 1 && editing_question.optionList[0].optionText.trim() == '')){
		alert('请先添加选项！');
		return;
	}
	questionnaire.questionList[editing_question_id] = editing_question;
	var inner = new String('');
	inner = '<input type="button" value="添加问题" onClick="addQuestion(-1)"/>';
	document.getElementById('questionnaire_edit_panel').innerHTML = inner;
	display_questionnaire();
	return;
}

function cacheOption(){
	editing_question.questionTitle = document.getElementById('qTitle').value;
	editing_question.questionType = document.getElementById('qType').value;
	var tlist = document.getElementsByName('edit_option');
	editing_question.optionList.splice(0, editing_question.optionList.length);
	for(var i = 0; i < tlist.length; i++){
		if(tlist[i].value.trim() == ''){
			continue;
		}
		var op = getNewOption();
		op.optionText = tlist[i].value;
		editing_question.optionList[i] = op;
	}
	if(editing_question.optionList.length == 0){
		editing_question.optionList[0] = getNewOption();
	}
	return;
}

function deleteQuestion(index){
	questionnaire.questionList.splice(index,1);
	display_questionnaire();
}

function questionUp(index){
	if(index == 0){
		return;
	}
	var temp = questionnaire.questionList[index-1];
	questionnaire.questionList[index-1] = questionnaire.questionList[index];
	questionnaire.questionList[index] = temp;
	display_questionnaire();
}

function display_edit_question(){
	var inner = new String('');
	inner = editQuestionTmpl;
	inner = inner.replace('#questionTitle#', editing_question.questionTitle);
	inner = inner.replace('#buttonValue#', (isNewQuestion ? '创建' : '修改'));
	inner = inner.replace('#questionType'+editing_question.questionType+'#', ' selected="selected"');
	inner = inner.replace(/#questionType\d+#/, '');
	optionList = new String('');
	for(var i = 0; i < editing_question.optionList.length; i++){
		var temp = editOptionTmpl;
		temp = temp.replace('#value#', editing_question.optionList[i].optionText)
		temp = temp.replace('#No#', i+1);
		optionList += temp;
	}
	inner = inner.replace('#optionList#', optionList);
	document.getElementById('questionnaire_edit_panel').innerHTML = inner;
}


function display_questionnaire(){
	var inner = new String('');
	inner = displayQuestionnaireTmpl;
	inner = inner.replace('#questionnaireName#', questionnaire.questionnaireName);
	inner = inner.replace('#questionnaireDescription#', questionnaire.questionnaireDescription);
	var qlist = new String('');
	for(var i = 0; i < questionnaire.questionList.length; i++){
		var tq = displayQuestionTmpl;
		tq = tq.replace('#questionTitle#', questionnaire.questionList[i].questionTitle);
		var ctrl = new String('');
		if(i != 0){
			ctrl += '<input type="button" onClick="questionUp(#questionIndex#)" value="上移">';
		}
		if(i != (questionnaire.questionList.length-1)){
			ctrl += '<input type="button" onClick="questionDown(#questionIndex#)" value="下移">';
		}
		tq = tq.replace('#ctrl#', ctrl);
		tq = tq.replace(new RegExp('#questionIndex#',"gm"), i);
		var olist = new String('');
		for(var j = 0; j < questionnaire.questionList[i].optionList.length; j++){
			var to = displayOptionTmpl;
			to = to.replace('#optionClass#', 'optionClass');
			to = to.replace('#optionType#', (questionnaire.questionList[i].questionType == 1 ? 'radio' : 'checkbox'));
			to = to.replace('#optionName#', 'question'+i);
			to = to.replace('#optionText#', questionnaire.questionList[i].optionList[j].optionText);
			olist += to;
		}
		tq = tq.replace('#optionList#', olist);
		qlist += tq;
	}
	inner = inner.replace('#questionList#', qlist);
	document.getElementById('questionnaire_display_panel').innerHTML = inner;
	return;
}


function getNewQuestionnaire(){
	var q = new Object();
	q.questionnaireName = new String('');
	q.questionnaireDescription = new String('');
	q.questionnaireType = 1;
	q.questionnaireGroup = 1;
	q.questionList = new Array();
	return q;
}

function getNewQuestion(){
	var q = new Object();
	q.questionTitle = new String('');
	q.questionType = 1;
	q.optionList = new Array();
	return q;
}

function getNewOption(){
	var q = new Object();
	q.optionText = new String('');
	return q;
}

function createQuestionnaire(){
	if(questionnaire.questionnaireName.trim() != '' && questionnaire.questionList.length != 0){
		var s = questionnaireToJSONString(questionnaire);
		submitJSON(s);
	}
	else{
		alert('问卷信息不完整！');
	}
}

function getQuestionnaire(userid){
	getJSON(userid);
}

function questionnaireToJSONString(questionnaire){
	var str = new String('');
	str = '{"questionnaireName":"' + questionnaire.questionnaireName + '","questionnaireDescription":"' + questionnaire.questionnaireDescription +
	'","questionnaireType":' + questionnaire.questionnaireType.toString() + ',"questionnaireGroup":' + questionnaire.questionnaireGroup.toString() +
	',"questionList":[';
	for (var i = 0; i < questionnaire.questionList.length; i++){
		if(i != 0){
			str += ',';
		}
		str += questionToJSONString(questionnaire.questionList[i]);
	}
	str += ']}';
	return str;
}

function questionToJSONString(question){
	var str = new String('');	
	str += '{"questionTitle":"' + question.questionTitle + '","questionType":' + questionnaire.questionnaireType.toString() + ',"optionList":[';
	for (var i = 0; i < question.optionList.length; i++){
		if(i != 0){
			str += ',{"optionText":"' + question.optionList[i].optionText + '"}';
		}
		else{
			str += '{"optionText":"' + question.optionList[i].optionText + '"}';
		}
	}
	str += ']}';
	return str;
}
var xmlHttp;
function submitJSON(str){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("您的浏览器不支持AJAX！");
		return;
	}
	var url="http://localhost:8080/vote/questionnaire/createQuestionnaire";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=submitStateChanged;
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(str);
}

function getJSON(str){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("您的浏览器不支持AJAX！");
		return;
	}
	var url="http://localhost:8080/vote/questionnaire/getQuestionnaire-";
	url=url+str;
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=getStateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("MSXML2.XMLHTTP.3.0");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function submitStateChanged() { 
	if(xmlHttp.readyState==4){ 
		document.getElementById("output").innerHTML=xmlHttp.responseText;
	}
}

function getStateChanged(){
	if(xmlHttp.readyState==4){
		questionnaire = eval('('+xmlHttp.responseText+')');
		var inner = new String('');
		inner = '<input type="button" value="添加问题" onClick="addQuestion(-1)"/>';
		document.getElementById('questionnaire_edit_panel').innerHTML = inner;
		display_questionnaire();
	}
}